<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="./src/Itcast.core.js"></script>
    <script src="./src/Itcast.dom.js"></script>
    <style>
        .dv {
            width: 300px;
            height: 100px;
            border: 1px solid red;
            margin: 10px 0;
        }
    </style>
    <script>

        var arr = [],
            push = arr.push;


        Itcast.fn.appendTo = function ( selector ) {
            var iObj = this.constructor( selector ); // 统一变成伪数组了
            // this 是 伪数组, iObj 也是伪数组, 将 this 中的每一个元素, 加到 iObj 中的每一个元素里
            var tmp = [], tmpNode;

            for ( var i = 0; i < this.length; i++ ) {

                for ( var j = 0; j < iObj.length; j++ ) {

                    tmpNode = j == iObj.length - 1 ? this[ i ] : this[ i ].cloneNode( true );

                    tmp.push( tmpNode );

                    iObj[ j ].appendChild( tmpNode );

                }

            }

            // 返回的是一个新的 jq 对象, 用 tmp 中的元素组成
            var tmpIobj = this.constructor();
            tmpIobj.prevObject = this;
            push.apply( tmpIobj, tmp );
            return tmpIobj;

        };


        Itcast.fn.end = function () {
            return this.prevObject || this.constructor();
        };
    </script>
</head>
<body>
    <div class="dv"></div>
    <div class="dv"></div>
    <div class="dv"></div>
    <div class="dv"></div>

</body>
<script>

    // var  iObj1 = I( '<div>1</div><div>2</div><div>3</div><div>4</div>' )
    
    // var iObj2 = iObj1.appendTo( '.dv' ).each(function () {
    //     this.style.border = '1px dashed blue';
    // });

    I( '<div>1</div><div>2</div><div>3</div><div>4</div>' ).appendTo( '.dv' ).each(function () {
        this.style.border = '1px dashed blue';
    }).end().each(function () {
        this.style.backgroundColor = 'pink';
    });

</script>
</html>